import 'package:flutter/material.dart';

class ChoiceChipDemo extends StatefulWidget {
  const ChoiceChipDemo({Key? key}) : super(key: key);

  @override
  _ChoiceChipDemoState createState() => _ChoiceChipDemoState();
}

class _ChoiceChipDemoState extends State<ChoiceChipDemo> {
  List<String> _list = ["Apple", "Banana", "Lemon"];

  // 被选中的列表
  String _chooseItem = "Lemon";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("ChoiceChip"),
      ),
      body: Container(
        padding: EdgeInsets.all(15),
        child: Column(children: [
          Text("选择的数据:$_chooseItem"),
          Wrap(
            spacing: 8,
            runSpacing: 8,
            children: _list.map((tag) {
              return ChoiceChip(
                selectedColor: Colors.green.shade300,
                label: Text(tag),
                selected: _chooseItem == tag,
                onSelected: (choose) {
                  setState(() {
                    _chooseItem = tag;
                  });
                },
              );
            }).toList(),
          )
        ]),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            _list = ["Apple", "Banana", "Lemon"];
            _chooseItem = "Lemon";
          });
        },
        child: Icon(Icons.restore),
      ),
    );
  }
}
